package com.leetomlee123.communication;

import com.leetomlee123.communication.server.ChatServer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.GetMapping;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @author lee
 */


@SpringBootApplication
public class CommunicationApplication implements CommandLineRunner {

    @Value("${socket.port}")
    private int port;

    @Autowired
    private ChatServer chatServer;


    public static void main(String[] args) {

        new SpringApplicationBuilder().web(WebApplicationType.REACTIVE).sources(CommunicationApplication.class).run(args);
    }

    @GetMapping
    public void c() {
        System.out.println("here");
    }

    @Override
    public void run(String... strings) {

        InetSocketAddress address = null;
        try {
            address = new InetSocketAddress(InetAddress.getLocalHost().getHostName(), port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        ChannelFuture future = chatServer.run(address);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> chatServer.destroy()));
        future.channel().closeFuture().syncUninterruptibly();
    }
}
